package com.example.pei.papa.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.pei.papa.tables.UserInfo;

import cn.bmob.sms.BmobSMS;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

import com.example.pei.papa.R;


public class Login_Activity extends AppCompatActivity implements View.OnClickListener{
    private CheckBox checkBox_displayPassword;
    private EditText editText_passWord;
    private EditText editText_userName;
    private Button button_login;
    private Button button_register;

    //登录的用户名和密码
    private String userName;
    private String passWord;

    private UserInfo user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setContentView(R.layout.activity_login_);
        InitView();

        Bmob.initialize(this, "43085233ef109100a09bc24120eb2c06");
        BmobSMS.initialize(Login_Activity.this,"43085233ef109100a09bc24120eb2c06");

        user = UserInfo.getCurrentUser(UserInfo.class);

        if(user != null){
            // 允许用户使用应用
            Intent intent=new Intent("main_Activity");
            Bundle bundle=new Bundle();
            bundle.putParcelable("currentUser", user);
            intent.putExtras(bundle);
            startActivity(intent);

            //销毁活动
            finish();

        }else{
            //缓存用户对象为空时， 可打开用户注册界面…
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                userName=editText_userName.getText().toString();
                passWord=editText_passWord.getText().toString();
                user=new UserInfo();
                user.setUsername(userName);
                user.setPassword(passWord);
                user.login(new SaveListener<UserInfo>() {
                    @Override
                    public void done(UserInfo bmobUser, BmobException e) {
                        if(e==null){
                            Toast.makeText(Login_Activity.this,"登录成功",Toast.LENGTH_SHORT).show();
                            Intent intent=new Intent("main_Activity");
                            Bundle bundle=new Bundle();
                            user = UserInfo.getCurrentUser(UserInfo.class);
                            bundle.putParcelable("currentUser", user);
                            intent.putExtras(bundle);
                            startActivity(intent);

                            //销毁活动
                            finish();

                            //通过BmobUser user = BmobUser.getCurrentUser()获取登录成功后的本地用户信息
                            //如果是自定义用户对象MyUser，可通过MyUser user = BmobUser.getCurrentUser(MyUser.class)获取自定义用户信息
                        }else{
                            Toast.makeText(Login_Activity.this,"用户名或者密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                break;
            case R.id.cb_displaypassword:
                if(checkBox_displayPassword.isChecked()) {
                    //显示明文密码 ，光标设置最后
                    editText_passWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    editText_passWord.setSelection(editText_passWord.getText().length());
                }else{
                    editText_passWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    editText_passWord.setSelection(editText_passWord.getText().length());
                }

                break;
            case R.id.btn_register:
                startActivity(new Intent("register_step_1_Activity"));
                break;
        }
    }

    private void InitView(){
        checkBox_displayPassword=(CheckBox)findViewById(R.id.cb_displaypassword);
        editText_passWord=(EditText)findViewById(R.id.et_password);
        editText_userName=(EditText)findViewById(R.id.et_username);
        button_login=(Button)findViewById(R.id.btn_login);
        button_register=(Button)findViewById(R.id.btn_register);

        //注册监听
        button_login.setOnClickListener(this);
        button_register.setOnClickListener(this);
        checkBox_displayPassword.setOnClickListener(this);
    }
}
